package com.andrew.markov.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;

import com.andrew.markov.process.util.FileArrayProvider;

import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;

public class MainForm extends JFrame {

	private static final long serialVersionUID = 200284929895806900L;
	private JPanel contentPane;
	private JTextField textField;
	public int tableDimension;

	/**
	 * Create the frame.
	 */
	public MainForm() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 598, 387);
		this.setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("Load initial matrix from file");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					System.out.println("Start to lad matrix from file");
					String[][] fileArray = FileArrayProvider.readTo2DArray("src/main/resources/array.txt", 4);//TODO: dimension from file
					
					ProbabilityTable probabilityTable = new ProbabilityTable(fileArray);
//					
					
				} catch (IOException e1) {
					System.out.println("IO Exception");
				}
			}
		});
		btnNewButton.setBounds(41, 146, 202, 67);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Input initial matrix");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				tableDimension = Integer.parseInt(textField.getText());
				setVisible(false);
				ProbabilityTable probabilityTable = new ProbabilityTable(tableDimension);
				probabilityTable.setVisible(true);
			}
		});
		btnNewButton_1.setBounds(335, 146, 202, 67);
		contentPane.add(btnNewButton_1);
		
		textField = new JTextField();
		textField.setBounds(398, 224, 76, 38);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblMarkovProcessDemo = new JLabel("Markov process DEMO program");
		lblMarkovProcessDemo.setForeground(SystemColor.textHighlight);
		lblMarkovProcessDemo.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblMarkovProcessDemo.setBounds(189, 26, 218, 51);
		contentPane.add(lblMarkovProcessDemo);
	}
}
